package playing.action;

import playing.PlantDataReader;
import playing.PlantLogin;
import playing.PlayerPlantReading;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class delete {
    public delete(int userId) {
        JFrame frame = new JFrame("删除植物");
        frame.setSize(300, 150);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        frame.add(panel);

        JLabel label = new JLabel("请输入要放生的植物的ID编号：");
        panel.add(label);

        JTextField textField = new JTextField(10);
        panel.add(textField);

        JButton deleteButton = new JButton("删除");
        panel.add(deleteButton);

        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int plantId = Integer.parseInt(textField.getText());
                boolean deleteSuccess = (new PlantDataReader().deletePlantById(plantId))&&(new PlayerPlantReading().deletePlantForUser(userId,plantId));

                if (deleteSuccess) {
                    JOptionPane.showMessageDialog(frame, "删除成功");
                } else {
                    JOptionPane.showMessageDialog(frame, "删除失败，请重新输入");
                }
            }
        });

        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new delete(1); // 假设用户ID为1
            }
        });
    }
}
